package com.chinaoryx.er;

import com.mysql.jdbc.Driver;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: hyman.zhang
 * Date: 12-1-28
 * Time:
 */
public class MyBatisTestUnit {
    @Test
    public void testMyBatis() throws ConfigurationException, SQLException, IOException {
        System.out.println("yyyyyyyyyyyyyyyyyyyy");
        Configuration config = null;

        config = new PropertiesConfiguration("config.properties");


        String url = config.getString("db.url");
        String username = config.getString("db.username");
        String password = config.getString("db.password");


            DriverManager.registerDriver(new Driver());

            Connection conn = DriverManager.getConnection(url, username, password);



            String resource = "configuration.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
                    .build(reader);


            SqlSession session = sqlMapper.openSession(conn);
            List list = session.selectList("prod.select");
            System.out.println(list);
            session.close();
            conn.close();

        Connection conn2 = DriverManager.getConnection(url, username, password);
        SqlSession session2 = sqlMapper.openSession(conn2);


        session2.close();
        conn2.close();

    }
}
